import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.dfs.DistributedFileSystem;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;


public class ProjectMain {

	/**
	 * assumptions: the values contains only in1, in2, or both in values
	 * 				if op is empty, in2 is also empty
	 * 
	 * @param in1	first word in the user's query
	 * @param op	operator is "AND" "OR" "ANDNOT" or empty (this is checked when user inputs query)
	 * @param in2	second word in the user's query
	 * @param values	iterator list of the relevant words at the docID:byteoffset
	 * 					a word is relevant because it is equal to either in1 or in2
	 * @return		true if the value list satisfies the query given
	 * 				false if value list rejects the query logic
	 */
	@SuppressWarnings("unchecked")
	public static boolean satisfiesQuery(String in1, String op, String in2, Iterator values){
		String valString = new String();
		if (op == null)
			return true; //values must contain in1 by definition
		while (values.hasNext()){
			valString=valString.concat(((Text)values.next()).toString());
		}
		if (op.compareTo("AND") == 0){
			if ((valString.indexOf(in1) >= 0) && (valString.indexOf(in2) >= 0))
				return true;			
		}
		else
			if (op.compareTo("OR") == 0)
				return true; //values must contain either in1 or in2 by definition
			else
				if (op.compareTo("ANDNOT") == 0){
					if ((valString.indexOf(in1) >= 0) && (valString.indexOf(in2) < 0))
						return true;
				}
		return false;	
	}


	public static DistributedFileSystem getDFS (JobConf conf)
	throws IOException {
		try {
			DistributedFileSystem fs = (DistributedFileSystem) (new JobClient(conf)).getFs();
			return fs;
		} catch (IOException e) {
			throw new RuntimeException ("I/O: " + e);
		}
	}

	public static String scrub(String word) {
		StringBuilder w = new StringBuilder (word);

		while (w.length()>0 && !Character.isLetterOrDigit(w.charAt(0)))
			w.deleteCharAt(0);

		while (w.length()>0 && !Character.isLetterOrDigit(w.charAt(w.length()-1)))  
			w.deleteCharAt(w.length()-1);

		return w.toString().toLowerCase();
	}

	public static String fileToString(Path dfsInput, DistributedFileSystem fs) {
		try {
			FSDataInputStream file = fs.open(dfsInput);
			ByteArrayOutputStream outStream = new ByteArrayOutputStream ();
			int c = file.read();
			while (c != -1) {
				outStream.write((byte) c);
				c = file.read();
			}
			return outStream.toString();
		} catch (IOException e) {
			throw new RuntimeException ("I/O: " + e);
		}
	}
/*
	public static String getSnippet (String position, DistributedFileSystem fs)
	throws NumberFormatException, IOException {
//		BufferedInputStream in = new BufferedInputStream (new FileInputStream
//				(position.substring(0, position.indexOf(':'))));
		FSDataInputStream in = fs.open(new Path (position.substring(0, position.indexOf(':'))));
		in.skip(Integer.parseInt(position.substring(position.indexOf(':')+1, position.length())));
		StringBuilder str = new StringBuilder();
		int b = in.read();
		while (b != -1 && b != '\n') {
			str.append((byte) b);
			b = in.read();
		}
		return str.toString();
	}
*/
//	public static void main (String[] args) throws NumberFormatException, IOException {
//		DistributedFileSystem dfs = getDFS(new JobConf (Query.class));
//		dfs.copyFromLocalFile(new Path ("Shakespeare"), new Path ("/"));
//		dfs.setWorkingDirectory(new Path ("/"));
//		System.out.println(getSnippet("/Shakespeare:102626", dfs));
//	}
}
